package cn.itsource.system.service.impl;

import cn.itsource.hrm.employeestate.EmployeeState;
import cn.itsource.hrm.paystate.PayState;
import cn.itsource.system.domain.Employee;
import cn.itsource.system.domain.Tenant;
import cn.itsource.system.dto.TenantDTO;
import cn.itsource.system.mapper.EmployeeMapper;
import cn.itsource.system.mapper.MealMapper;
import cn.itsource.system.mapper.TenantMapper;
import cn.itsource.system.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zd
 * @since 2020-02-21
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
//保存注入信息

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private MealMapper mealMapper;
    @Override
    public void entering(TenantDTO tenantDTO) {

        Date date=new Date();
        Tenant tenant = tenantDTO.getTenant();
        //第一步保存租户信息
        //获得返回的id
        //设置入住时间
        tenant.setRegisterTime(date);
         baseMapper.insert(tenant);
//注意这个地方保存过后，就会有id，因为会返回主键id
        //保存员工
        Employee employee = tenantDTO.getEmployee();
        //设置创建时间
        employee.setInputTime(date);
        //设置状态
        employee.setState(EmployeeState.EMPLOYEE_NORMAL);
        //设置租户的id
        employee.setTenantId(tenant.getId());
        Integer insert1 = employeeMapper.insert(employee);
        //保存机构和套餐的中间表  机构和租户中间表
        //获得套餐的id
        Long mealId = tenantDTO.getMealId();
        mealMapper.insertAgencyTenantTable(mealId,tenant.getId(), PayState.PAY_UNPAID);
}
}
